package com.sfdk.graphics.engine.data
{
import com.sfdk.data.api.IImageData;
import com.sfdk.data.api.ITreeDataProvider;
import com.sfdk.graphics.engine.api.ITileset;
import com.sfdk.io.GenericIO;
import com.sfdk.pools.BitmapDataPool;

import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.clearInterval;
import flash.utils.setInterval;

	public class Tileset extends GenericIO implements ITileset, ITreeDataProvider, IImageData
	{
		private var BASE_RECT : Rectangle;
		private const BASE_POINT : Point = new Point();
        private var _delayInit:int = -1;
		public function Tileset(currentDirectory:String = null, filePath:String = null, id : int = -1, tileSize : int = 24)
		{
             _fullName = filePath;
            if (filePath != null) {
                var pathArray:Array = filePath.split("/");
                _type = pathArray[pathArray.length - 2];
                _name = pathArray[pathArray.length - 1];
            }

			_tileSize = tileSize;
			_id = id;
			_fileName = filePath;
			BASE_RECT = new Rectangle(0, 0, _tileSize, _tileSize);
            _imageData = BitmapDataPool.instance.getFromPool(filePath)
            if(_imageData != null)
            {
			    currentDirectory = null;
                filePath = null;
                _delayInit = setInterval(init, 20);
            }
            super(currentDirectory + filePath, GenericIO.BITMAP_FILE);
		}
		
		//-------------------------------------------------------
		//
		//  properties
		//
		//-------------------------------------------------------

        private var _fullName : String;
        public function get fullName():String {
            return _fullName;
        }

        private var _name : String;
        public function get label():String {
            return _name;
        }

        private var _type : String;
        public function get type():String {
            return _type;
        }

        public function get rootType():String {
            return "";
        }

        public function get isImage():Boolean {
            return true;
        }

        public function get imageData():BitmapData {
            return _imageData;
        }

        //----------------------------------
		// fileName
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _fileName : String;
		
		public function get fileName() : String
		{
			return _fileName;
		}
		
		//----------------------------------
		// id
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _id : int;
		
		public function get id() : int
		{
			return _id;
		}

        public function set id(value : int) : void
		{
			_id = value;
		}
		
		//----------------------------------
		// tileSize
		//----------------------------------
		
		/**
		 * @private
		 */
		private var _tileSize : int;
		
		public function get tileSize() : int
		{
			return _tileSize;
		}
		
		//----------------------------------
		// tileBitmap
		//----------------------------------
		
		/**
         * @private
         */
        private var _tileBitmap:BitmapData;
		
		//----------------------------------
		// cuttingRect
		//----------------------------------
		
		/**
         * @private
         */
        private var _cuttingRect:Rectangle;
		
		//----------------------------------
		// tilePerRow
		//----------------------------------
		
		/**
         * @private
         */
        private var _tilePerRow:int;
		
		//----------------------------------
		// tilesetWidth
		//----------------------------------
		
		/**
         * @private
         */
        private var _tilesetWidth:int;
		
		//----------------------------------
		// tilesetHeight
		//----------------------------------
		
		/**
         * @private
         */
        private var _tilesetHeight:int;
		
		//-------------------------------------------------------
		//
		//  ITileset methods
		//
		//-------------------------------------------------------
		
		public function getData() : BitmapData
		{
			return _imageData;
		}
		
		public function getTileById(id : int) : BitmapData
		{
			_cuttingRect.x = (id  - _tilePerRow * int(id / _tilePerRow)) * _tileSize;
			_cuttingRect.y = int(id / _tilePerRow) * _tileSize;
			_cuttingRect.width = _tileSize;
			_cuttingRect.height = _tileSize;
			
			_tileBitmap.fillRect(BASE_RECT, 0);
			_tileBitmap.copyPixels(_imageData, _cuttingRect, BASE_POINT);
			
			return _tileBitmap;
		}
		
		public function getTile(tileX : int, tileY : int) : BitmapData
		{
			_cuttingRect.x = tileX * _tileSize;
			_cuttingRect.y = tileY * _tileSize;
			_cuttingRect.width = _tileSize;
			_cuttingRect.height = _tileSize;
			
			_tileBitmap.fillRect(_cuttingRect, 0);
			_tileBitmap.copyPixels(_imageData, _cuttingRect, BASE_POINT);
			
			return _tileBitmap;
		}
		
		public function getTileRect(pointRect : Rectangle) : BitmapData
		{
			_cuttingRect.x = pointRect.top * _tileSize;
			_cuttingRect.y = int(pointRect.left / _tilePerRow) * _tileSize;
			_cuttingRect.width = pointRect.right * _tileSize;
			_cuttingRect.height = int(pointRect.bottom / _tilePerRow) * _tileSize;
			
			var bit : BitmapData = new BitmapData(_tileSize * (_cuttingRect.width - _cuttingRect.x), _tileSize * (_cuttingRect.height - _cuttingRect.y));			
			bit.copyPixels(_imageData, _cuttingRect, BASE_POINT);
			
			return bit;
		}
		
		public function emptyTileset() : void
		{
			if(_fileName.indexOf("walkableTileset.png") == -1)
				_imageData.dispose();
		}
		
		protected override function init() : void
		{
            if(_delayInit != -1)
                clearInterval(_delayInit);
            BitmapDataPool.instance.addToPool(_imageData, _fullName);
			_tilesetWidth = _imageData.width;
			_tilesetHeight = _imageData.height;
			
			_tilePerRow = int(Math.ceil(_tilesetWidth / _tileSize));
			_cuttingRect = new Rectangle(0, 0, _tileSize, _tileSize);
			_tileBitmap = new BitmapData(_tileSize, _tileSize);
			
			dispatchEvent(new Event("tilesetLoaded"));
		}
	}
}